package zozopo.user.operations;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;

import zozopo.comm.Encrypt;
import zozopo.comm.mail.MailSender;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;
import zozopo.user.RandomTB;
import zozopo.user.USERCODE;
import zozopo.warehouse.operations.GoodsTB;

public class UserOS extends OperatingService{
	
	private static final Log logger = LogFactory.getLog(UserOS.class);
	
	public UserOS(Connection conn){
		super(conn);
		
	}
	
	public UserOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	/**
	 * @version 1.0
	 * @param String username,---> 用户名
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see isExist(String operator,String password)--->判断用户是否存在
	 */
	
	public boolean isExist(String username) throws IOException{
		boolean eResult = false;
		
		String sql = "select * from "+UserTB.TABLE_NAME+" where "+UserTB.NAME_USER+"='"+username+"'";
		
		if(logger.isDebugEnabled()){
			logger.debug("isExist() sql:"+sql.toString());
		}
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				eResult = true;
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @param String username,---> 用户名
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see isExist(String operator,String password)--->判断用户是否存在
	 */
	
	public boolean isExist(String email,String qq) throws IOException{
		boolean eResult = false;
		
		String sql = "select * from "+UserTB.TABLE_NAME+" where "+UserTB.EMAIL_USER+"='"+email+"'" + " and " + UserTB.QQ_USER  +"='" + qq + "'";
		
		//System.out.println(sql);
		if(logger.isDebugEnabled()){
			logger.debug("isExist() sql:"+sql.toString());
		}
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				eResult = true;
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @param String username,---> 用户名
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see isExist(String operator,String password)--->判断用户是否存在
	 */
	
	public boolean isExistQQ(String qq) throws IOException{
		boolean eResult = false;
		
		String sql = "select * from "+UserTB.TABLE_NAME+" where "+UserTB.QQ_USER+"='"+qq+"'";
		
		if(logger.isDebugEnabled()){
			logger.debug("isExist() sql:"+sql.toString());
		}
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				eResult = true;
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @param String username,---> 用户名
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see isExist(String operator,String password)--->判断用户是否存在
	 */
	
	public boolean isLockedEmail(String email) throws IOException{
		boolean eResult = false;
		
		String sql = "select * from "+UserTB.TABLE_NAME+" where "+UserTB.EMAIL_USER+"='"+email+"'"
		+ " AND " + UserTB.EMAILLOCKED_USER + "='"+USERCODE.EMAILLOCKED+"'";
		
		if(logger.isDebugEnabled()){
			logger.debug("isExist() sql:"+sql.toString());
		}
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				eResult = true;
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @param String username,---> 用户名
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see isExist(String operator,String password)--->判断用户是否存在
	 */
	
	public boolean isExistForVali(String username,String qq) throws IOException{
		boolean eResult = false;
		
		String sql = "select * from "+UserTB.TABLE_NAME+" where "+UserTB.NAME_USER+"='"+username+"'";
		
		if(logger.isDebugEnabled()){
			logger.debug("isExist() sql:"+sql.toString());
		}
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				eResult = true;
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @param String username,---> 用户名
	 * @author Shea
	 * @return boolean  
	 * @throws IOException 
	 * @see isExist(String operator,String password)--->判断用户是否存在
	 */
	
	public boolean isActive(String email) throws IOException{
		boolean eResult = false;
		
		String sql = "select * from "+UserTB.TABLE_NAME+" where "+UserTB.EMAIL_USER+"='"+email+"'";
		
		if(logger.isDebugEnabled()){
			logger.debug("isExist() sql:"+sql.toString());
		}
		try {
			ResultSet result = databaseConnection.executeQuery(conn,sql);
		
			if(result.next()){
				String sate = result.getString(UserTB.STATE_USER);
				if(sate.equals("1")){
					eResult = true;
				}
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean upadteUser(UserTB user){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		//updateMap.put(UserTB.USER_STORE, user.getUser_store());
	//	updateMap.put(UserTB.USER_REMARK, user.getUser_remark());
		
		return sqlMachine.updateRow(UserTB.TABLE_NAME, updateMap, UserTB.EMAIL_USER, user.getEmail_user());
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean registUser(UserTB user){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		insertMap.put(UserTB.QQ_USER, user.getQq_user());
		insertMap.put(UserTB.EMAIL_USER, user.getEmail_user());
		insertMap.put(UserTB.NAME_USER, user.getName_user());
		insertMap.put(UserTB.PASSWORD_USER, Encrypt.MessageDigest(user.getPassword_user()));
		insertMap.put(UserTB.STATE_USER, "0");
		
		return sqlMachine.insertRow(UserTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 0.2
	 * @author Shea
	 */
	public UserTB getUserInfoByName(String username){
		username = "'"+username+"'";
		String sql = "select * from " + UserTB.TABLE_NAME + " where " +UserTB.NAME_USER+"="+username;
	
		if(logger.isDebugEnabled()){
			logger.debug("getUserInfoByName() sql:"+sql.toString());
		}
		UserTB user = new UserTB();
		List<UserTB> userList = selectBySql(sql);
		if(userList!=null){
			if(userList.size()>=1){
				user = userList.get(0);
			}else{
				user = null;
			}
		}
		
		return user;
	}
	
	/**
	 * @version 0.2
	 * @author Shea
	 */
	public UserTB getUserInfoByQQ(String qq){
		qq = "'"+qq+"'";
		String sql = "select * from " + UserTB.TABLE_NAME + " where " +UserTB.QQ_USER+"="+qq;
	
		if(logger.isDebugEnabled()){
			logger.debug("getUserInfoByName() sql:"+sql.toString());
		}

		return getUserInfoBySql(sql);
	}
	
	/**
	 * @version 1
	 * @author Gee
	 */
	public UserTB getUserInfo(int id){
		
		String sql = "select * from " + UserTB.TABLE_NAME + " where " +UserTB.ID_USER+"="+id;
	
		if(logger.isDebugEnabled()){
			logger.debug("getUserInfoByName() sql:"+sql.toString());
		}

		return getUserInfoBySql(sql);
	}
	
	/**
	 * @version 0.2
	 * @author Shea
	 */
	public UserTB getUserInfoByEmail(String email){
		
		email = "'"+email+"'";
		String sql = "select * from " + UserTB.TABLE_NAME + " where " +UserTB.EMAIL_USER+"="+email
		+ " AND "+UserTB.EMAILLOCKED_USER+"='"+USERCODE.EMAILLOCKED+"'";
		
		UserTB user = new UserTB();
		List<UserTB> userList = selectBySql(sql);
		user = userList.get(0);
		
		if(logger.isDebugEnabled()){
			logger.debug("getUserInfoByEmail() sql:"+sql.toString());
		}
		
		return user;
	}
	
	/**
	 * @version 0.2
	 * @author Shea
	 */
	public UserTB getUserInfoBySql(String sql){
		UserTB userInfo = null;
		
		System.out.println(sql);
		if(logger.isDebugEnabled()){
			logger.debug("getUserInfoByName() sql:"+sql.toString());
		}
		ResultSet resultSet = databaseConnection.executeQuery(conn,sql);
		
		try {
			if(resultSet.next()){
				userInfo = new UserTB(); 
				
				
				
				userInfo.setAddress_operator(resultSet.getString(UserTB.ADDRESS_OPERATOR));
				userInfo.setDes_user(resultSet.getString(UserTB.DES_USER));
				userInfo.setEmail_user(resultSet.getString(UserTB.EMAIL_USER));
				userInfo.setEmaillocked_user(resultSet.getString(UserTB.EMAILLOCKED_USER));
				userInfo.setId_user(resultSet.getInt(UserTB.ID_USER));
				//userInfo.setLastlogindate_user(resultSet.getString(UserTB.LASTLOGINDATE_USER));
				userInfo.setLastname_user(resultSet.getString(UserTB.LASTNAME_USER));
				userInfo.setLimitedflag_user(resultSet.getString(UserTB.LIMITEDFLAG_USER));
				userInfo.setMobile_operator(resultSet.getString(UserTB.MOBILE_OPERATOR));
				userInfo.setName_user(resultSet.getString(UserTB.NAME_USER));
				userInfo.setNickname_user(resultSet.getString(UserTB.NICKNAME_USER));
				userInfo.setPhone_operator(resultSet.getString(UserTB.PHONE_OPERATOR));
				userInfo.setQq_user(resultSet.getString(UserTB.QQ_USER));
				userInfo.setRealname_user(resultSet.getString(UserTB.REALNAME_USER));
				userInfo.setReigstdate_user(resultSet.getString(UserTB.REIGSTDATE_USER));
				userInfo.setState_user(resultSet.getString(UserTB.STATE_USER));

				
				resultSet.close();
			}
		} catch (SQLException e) {
				// TODO Auto-generated catch block
			e.printStackTrace();
				
		} 
			
		
		return userInfo;
	}
	
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean isRealUser(String username,String password){
		String sql = "select * from "+UserTB.TABLE_NAME+" where "+UserTB.NAME_USER+"='"+username
		+"' and "+UserTB.PASSWORD_USER+"='"+Encrypt.MessageDigest(password)+"'";
		
		boolean eResult = false;
		try {
			ResultSet result = databaseConnection.executeQuery(conn, sql);
			if(result.next()){
				eResult = true;
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean isRealUserByEmail(String email,String passowrd) {
		String sql = "select * from "+UserTB.TABLE_NAME
		+" where "+UserTB.EMAIL_USER+"='"+email+"' " 
		+ " AND "+UserTB.PASSWORD_USER+"='"+Encrypt.MessageDigest(passowrd)+"'"
		+ " AND "+UserTB.EMAILLOCKED_USER+"='"+USERCODE.EMAILLOCKED+"'";

		
		System.out.println(sql.toString());
		if(logger.isDebugEnabled()){
			logger.debug("isRealUserByEmail() sql:"+sql.toString());
		}
		boolean eResult = false;
		try {
			ResultSet result = databaseConnection.executeQuery(conn, sql);
			if(result.next()){
				eResult = true;
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	

	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<UserTB> selectBySearchStr(String searchStr,int beginPageNum,int pageSize){
		String sql = "select * from "+UserTB.TABLE_NAME+" WHERE  "+UserTB.EMAIL_USER+" LIKE '%"+searchStr+"%'"+" OR "+UserTB.NAME_USER+" LIKE '%"+searchStr+"%'"+" limit "+beginPageNum+","+pageSize; 
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		return selectBySql(sql);
	}
	

	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<UserTB> selectBySql(String sql){
		//String sql = "select * from "+UserTB.TABLE_NAME+" order by  "+UserTB.USER_PRESTIGE+" desc limit "+beginPageNum+","+pageSize; 
		System.out.println(sql);
		List<UserTB> resultList = null;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
			try {
				resultList = new ArrayList<UserTB>();
				while(resultSet.next()){
					UserTB userInfo = new UserTB();
					
					userInfo.setAddress_operator(resultSet.getString(UserTB.ADDRESS_OPERATOR));
					userInfo.setDes_user(resultSet.getString(UserTB.DES_USER));
					userInfo.setEmail_user(resultSet.getString(UserTB.EMAIL_USER));
					userInfo.setEmaillocked_user(resultSet.getString(UserTB.EMAILLOCKED_USER));
					userInfo.setId_user(resultSet.getInt(UserTB.ID_USER));
					//userInfo.setLastlogindate_user(resultSet.getString(UserTB.LASTLOGINDATE_USER));
					userInfo.setLastname_user(resultSet.getString(UserTB.LASTNAME_USER));
					userInfo.setLimitedflag_user(resultSet.getString(UserTB.LIMITEDFLAG_USER));
					userInfo.setMobile_operator(resultSet.getString(UserTB.MOBILE_OPERATOR));
					userInfo.setName_user(resultSet.getString(UserTB.NAME_USER));
					userInfo.setNickname_user(resultSet.getString(UserTB.NICKNAME_USER));
					userInfo.setPhone_operator(resultSet.getString(UserTB.PHONE_OPERATOR));
					userInfo.setQq_user(resultSet.getString(UserTB.QQ_USER));
					userInfo.setRealname_user(resultSet.getString(UserTB.REALNAME_USER));
					userInfo.setReigstdate_user(resultSet.getString(UserTB.REIGSTDATE_USER));
					userInfo.setState_user(resultSet.getString(UserTB.STATE_USER));
					
					resultList.add(userInfo);
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return resultList;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean foundPassowrd(String activeCode,String qq){
		SqlMachine sqlMachine = new SqlMachine(conn);
		ResultSet result = sqlMachine.QueryRow(RandomTB.TABLE_NAME, RandomTB.RANDOM_VALUE, activeCode);
		boolean eResult = false;
		try {
			if(result.next()){
				String key = result.getString(RandomTB.RANDOM_KEY);
				String code = result.getString(RandomTB.RANDOM_VALUE);
				if(key.equals(qq) && code.equals(activeCode)){
					sqlMachine.updateRow(UserTB.TABLE_NAME, UserTB.QQ_USER, qq, UserTB.STATE_USER, "1");
					sqlMachine.deleteRow(RandomTB.TABLE_NAME, RandomTB.RANDOM_KEY, qq);
					eResult = true;
				}
				result.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			eResult = false;
		}
		
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean createFoundPassowrdCode(UserTB user) throws NoSuchAlgorithmException{
		StringBuffer activeCode = new StringBuffer();
		
		Long randomNum = Math.round(Math.random()*3333);
		MessageDigest md = MessageDigest.getInstance("MD5"); 
		md.update(randomNum.toString().getBytes());
		byte[] b = md.digest();
		b.toString();

		for(byte bs : b){
			activeCode.append(bs);
		}
		
		if(logger.isDebugEnabled()){
			logger.debug("createFoundPassowrdCode() qq:"+user.getQq_user() +"active:"+activeCode.toString());
		}
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		sqlMachine.deleteRow(RandomTB.TABLE_NAME, RandomTB.RANDOM_KEY, user.getEmail_user());
		
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		//insertMap.put(RandomTB.RANDOM_ID, sqlMachine.getMaxId(RandomTB.RANDOM_ID, RandomTB.TABLE_NAME));
		insertMap.put(RandomTB.RANDOM_KEY, user.getEmail_user());
		insertMap.put(RandomTB.RANDOM_VALUE, activeCode.toString());
		
		MailSender mailSender = new MailSender();
		
		mailSender.sendPasswordUrlToUser(user,activeCode.toString());
		
		sqlMachine.insertRow(RandomTB.TABLE_NAME, insertMap);
		return true; 
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean modifyPassowrdByCode(String code,String qq,String newpassword) throws IOException{
		//boolean eResult = false;
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		
		ResultSet result = sqlMachine.QueryRow(RandomTB.TABLE_NAME, RandomTB.RANDOM_VALUE, code);
		boolean eResult = false;
		try {
			if(result.next()){
				String key = result.getString(RandomTB.RANDOM_KEY);
				String value = result.getString(RandomTB.RANDOM_VALUE);
				if(key.equals(qq) && value.equals(code)){
					sqlMachine.updateRow(UserTB.TABLE_NAME, UserTB.QQ_USER, qq, UserTB.PASSWORD_USER, Encrypt.MessageDigest(newpassword));
					sqlMachine.deleteRow(RandomTB.TABLE_NAME, RandomTB.RANDOM_KEY, qq);
					eResult = true;
				}
				result.close();
				
				eResult = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			eResult = false;
		}
		
		
		return eResult;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + UserTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStr(String searchStr){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + UserTB.TABLE_NAME +" WHERE  "+UserTB.EMAIL_USER+" LIKE '%"+searchStr+"%'"+" OR "+UserTB.NAME_USER+" LIKE '%"+searchStr+"%'";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean updatePassowrdByEmial(String email,String newpassword){
		SqlMachine sqlMachine = new SqlMachine(conn);
		return sqlMachine.updateRow(UserTB.TABLE_NAME, UserTB.EMAIL_USER, email, UserTB.PASSWORD_USER, Encrypt.MessageDigest(newpassword));
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean updateLimitByEmail(String email,String limitflag) {
		SqlMachine sqlMachine = new SqlMachine(conn);
		
		//String limitflag = "";
		
		return sqlMachine.updateRow(UserTB.TABLE_NAME, UserTB.EMAIL_USER, email, UserTB.LIMITEDFLAG_USER, limitflag);
		
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 * @see userFlagLeve(UserTB user,int flag,int leve)
	 */
	
	public static boolean userFlagLeve(UserTB user,int flag,int leve){
		if(flag > 128){
			return false;
		}
		if(leve > 9){
			return false;
		}
		//user.getUser_limitflag();
		Character limitflag = user.getLimitedflag_user().charAt(flag);
		Integer intLimitflag = Integer.parseInt(limitflag.toString());
		
		if(intLimitflag >= leve){
			return true;
		}else{
			return false;
		}
		
		
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 
	 */
	
	public static String flagLeveForCheckBox(String flagStr,int flag,int leve){
		String checked = "";
		if(flag > 128){
			return "";
		}
		if(leve > 9){
			return "";
		}
		//user.getUser_limitflag();
		Character limitflag = flagStr.charAt(flag);
		Integer intLimitflag = Integer.parseInt(limitflag.toString());
		
		if(intLimitflag >= leve){
			checked = "checked";
		}
		
		return checked;
		
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 * @see 
	 */
	
	public static String jsonLimitFlagToUserLimitFlag(String jsonlimitflag,String userlimitflag){
		StringBuffer limitflag = new StringBuffer();
		
		JSONObject jo;
		try {
			jo = new JSONObject(jsonlimitflag);
			
			char[] limitChar = userlimitflag.toCharArray();
			
			HashMap<String,String> hm = jo.getHashMap();
			
			for(Entry<String,String> entry:hm.entrySet()){
				if(entry.getValue().toLowerCase().equals("false")){
					//entry.setValue(0);
					entry.setValue("0");
				}else if(entry.getValue().toLowerCase().equals("true")){
					entry.setValue("1");
				}
				limitChar[Integer.parseInt(entry.getKey())] = entry.getValue().charAt(0);
			}
			
			for(char c:limitChar){
				limitflag.append(c);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return limitflag.toString();
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean activeUser(String activeCode,String email,int externalid){
		SqlMachine sqlMachine = new SqlMachine(conn);
		ResultSet result = sqlMachine.QueryRow(RandomTB.TABLE_NAME, RandomTB.RANDOM_VALUE, activeCode);
		boolean eResult = false;
		try {
			if(result.next()){
				String key = result.getString(RandomTB.RANDOM_KEY);
				String code = result.getString(RandomTB.RANDOM_VALUE);
				int externalid_random = result.getInt(RandomTB.RANDOM_EXTERNALID);
				
				if(key.equals(email) && code.equals(activeCode) && externalid==externalid_random){
					sqlMachine.updateRow(UserTB.TABLE_NAME, UserTB.ID_USER, externalid ,UserTB.EMAILLOCKED_USER, USERCODE.EMAILLOCKED);
					sqlMachine.updateRow(UserTB.TABLE_NAME, UserTB.ID_USER, externalid, UserTB.STATE_USER, USERCODE.ACTIVATED);
					sqlMachine.deleteRow(RandomTB.TABLE_NAME, RandomTB.RANDOM_KEY, email);
					eResult = true;
				}
				result.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			eResult = false;
		}
		
		return eResult;
	}
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	
	public boolean createActiveNumber(UserTB user) throws IOException, NoSuchAlgorithmException{
		StringBuffer activeCode = new StringBuffer();
		
		Long randomNum = Math.round(Math.random()*3333);
		MessageDigest md = MessageDigest.getInstance("MD5"); 
		md.update(randomNum.toString().getBytes());
		byte[] b = md.digest();
		b.toString();

		for(byte bs : b){
			activeCode.append(bs);
		}
		
		if(logger.isDebugEnabled()){
			logger.debug("createActiveNumber() qq:"+user.getName_user() +"active:"+activeCode.toString());
		}
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		sqlMachine.deleteRow(RandomTB.TABLE_NAME, RandomTB.RANDOM_KEY, user.getId_user());
		
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		//insertMap.put(RandomTB.RANDOM_ID, sqlMachine.getMaxId(RandomTB.RANDOM_ID, RandomTB.TABLE_NAME));
		insertMap.put(RandomTB.RANDOM_KEY, user.getEmail_user());
		insertMap.put(RandomTB.RANDOM_EXTERNALID, user.getId_user());
		insertMap.put(RandomTB.RANDOM_VALUE, activeCode.toString());
		
		MailSender mailSender = new MailSender();
		mailSender.sendActiveUrltoUser(user,activeCode.toString());
		
		sqlMachine.insertRow(RandomTB.TABLE_NAME, insertMap);
		return true;
	}
	
	
}
